// ===============================================
// app_main_page
//
// Create by Will on 2020/10/5 5:23 PM
// Copyright @flutter_base.All rights reserved.
// ===============================================

import 'package:auto_route/auto_route.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base/const/app_theme.dart';
import 'package:flutter_base/generated/l10n.dart';
import 'package:flutter_base/page/chats/chats_page.dart';
import 'package:flutter_base/page/contacts/contacts_page.dart';
import 'package:flutter_base/page/discover/discover_page.dart';
import 'package:flutter_base/page/me/me_page.dart';

class AppMainPage extends StatefulWidget {
  @override
  _AppMainPageState createState() => _AppMainPageState();
}

class _AppMainPageState extends State<AppMainPage> {
  //默认打开的 tab 页码
  int _page = 0;

  //默认选择的 tab 页码
  int _selectIndex = 0;

  // tab栏页码切换
  void _handlePageChanged(int index) {}

  CupertinoTabController _controller;

  // 底部导航
  Widget _buildBottomNavigationBar(BuildContext context) {
    return CupertinoTabBar(
      items: [
        new BottomNavigationBarItem(
          icon: Icon(CupertinoIcons.house_alt,),
          activeIcon: Icon(CupertinoIcons.house_alt_fill,),
          label: AppS.of(context).chats,
          backgroundColor: CupertinoTheme.of(context).barBackgroundColor,
        ),
        new BottomNavigationBarItem(
          icon: Icon(CupertinoIcons.person_3,),
          activeIcon: Icon(CupertinoIcons.person_3_fill,),
          label: AppS.of(context).contacts,
          backgroundColor: CupertinoTheme.of(context).barBackgroundColor,
        ),
        new BottomNavigationBarItem(
          icon: Icon(CupertinoIcons.arrow_2_circlepath_circle,),
          activeIcon: Icon(CupertinoIcons.arrow_2_circlepath_circle_fill,),
          label: AppS.of(context).discover,
          backgroundColor: CupertinoTheme.of(context).barBackgroundColor,
        ),
        new BottomNavigationBarItem(
          icon: Icon(CupertinoIcons.person,),
          activeIcon: Icon(CupertinoIcons.person_fill,),
          label: AppS.of(context).me,
          backgroundColor: CupertinoTheme.of(context).barBackgroundColor,
        ),
      ],
      currentIndex: _page,
      onTap: _handlePageChanged,
      activeColor: themeOf(context).tabActiveColor,
      border: Border(
        top: BorderSide(
          color: CupertinoColors.white,
          width: 0.0, // One physical pixel.
          style: BorderStyle.solid,
        ),
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = new CupertinoTabController();
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoTheme(
      data: appCupertinoThemeData(MediaQuery.of(context).platformBrightness),
      child: CupertinoTabScaffold(
          tabBar: _buildBottomNavigationBar(context),
          controller: _controller,
          tabBuilder: (context, index) {
            CupertinoTabView returnValue;
            return CupertinoTabView(
              builder: (context) {
                switch (index) {
                  case 0:
                    returnValue = CupertinoTabView(builder: (context) {
                      return ChatsPage();
                    });
                    break;
                  case 1:
                    returnValue = CupertinoTabView(builder: (context) {
                      return ContactsPage();
                    });
                    break;
                  case 2:
                    returnValue = CupertinoTabView(builder: (context) {
                      return DiscoverPage();
                    });
                    break;
                  case 3:
                    returnValue = CupertinoTabView(builder: (context) {
                      return MePage();
                    });
                    break;
                }
                return returnValue;
              },
            );
          }),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.removeListener(_onListenerTabIndex);
    _controller.dispose();

    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    debugPrint("--" + state.toString());
    switch (state) {
      case AppLifecycleState.inactive: // 处于这种状态的应用程序应该假设它们可能在任何时候暂停。
        break;
      case AppLifecycleState.resumed: // 应用程序可见，前台
        // startTime();
        break;
      case AppLifecycleState.paused: // 应用程序不可见，后台
        // stopTime();
        break;
      case AppLifecycleState.detached: // 申请将暂时暂停
        break;
    }
  }


  _enterSignInPage() {
    // ExtendedNavigator.of(context).pushSignInPage();
  }


  _onListenerTabIndex() async {
//     // 读取用户配置
//     final user =
//         Provider.of<SugarParkUserProvider>(context, listen: false).value;
//
// //    if (_controller.index > 1 &&
// //        Provider.of<SugarParkUserProvider>(context, listen: false).value ==
// //            null) {
// //      // 如果是点击的 《聊天》 或者《我的》 用户要强制登陆
// //      _controller.index = _selectIndex;
// //      _showLoginDialog();
// //    } else {
// //      _selectIndex = _controller.index;
// //    }
//
//     if (_controller.index > 0 && user == null) {
//       // 如果是点击的 《聊天》 或者《我的》 用户要强制登陆
//       _controller.index = _selectIndex;
//       _showLoginDialog();
//     } else {
//       _selectIndex = _controller.index;
//     }
  }
}
